package com.nativetalk.service.three.live.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtLikes;
import com.nativetalk.bean.comment.TdNtLiveCommentList;
import com.nativetalk.bean.coupon.TdNtCouponDetail;
import com.nativetalk.bean.dict.TsNtDictIndex;
import com.nativetalk.bean.giving.TsNtLiveGivingList;
import com.nativetalk.bean.live.*;
import com.nativetalk.bean.live.his.TdNtLiveCourseHisMembList;
import com.nativetalk.bean.live.his.TdNtLiveCourseHisNewList;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/6/14.
 */
@Service
public class ThreeLiveSearchServiceImpl implements ThreeLiveSearchService{
    private static Logger logger = Logger.getLogger(ThreeLiveSearchServiceImpl.class);
    @Resource
    private TdNtLiveCommentDao tdNtLiveCommentDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtLivePictureDao tdNtLivePictureDao;
    @Resource
    private TdNtLiveStudyDao tdNtLiveStudyDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Resource
    private TdNtCouponDao tdNtCouponDao;
    @Resource
    private TdNtReserveTimeDao tdNtReserveTimeDao;
    @Resource
    private TdNtLikesDao tdNtLikesDao;
    @Resource
    private TdNtTeacherLabelDao tdNtTeacherLabelDao;
    @Resource
    private TdNtLiveCourseDao tdNtLiveCourseDao;
    @Override
    public RetInfo findLiveDetail(int live_cou_his_id, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findLiveDetail:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * 查找直播详情(id,封面,直播名称,时长 ,费用,简介,教师头像,名称,评分,频道)
             */
            TdNtLiveCourseHisDetails tdNtLiveCourseHisDetails = tdNtLiveCourseHisDao.findTdNtLiveCourseHisDetails(live_cou_his_id);
            if (tdNtLiveCourseHisDetails != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("live", tdNtLiveCourseHisDetails);
                /**
                 * 已购买直播人数,直播人头像
                 */
                List<TdNtLiveHisMemberHead> liveHisMemberHeads = tdNtLiveStudyDao.findTdNtLiveHisMemberHeads(live_cou_his_id);
                map.put("memb_head", liveHisMemberHeads);
                /**
                 * 是否购买改直播
                 */
                map.put("is_but", 1);
                if (tdNtMember != null ){
                    Map<String, Integer> params = new HashMap<>();
                    params.put("live_cou_his_id", live_cou_his_id);
                    params.put("memb_id", tdNtMember.getMemb_id());
                    TdNtLiveStudyDetail tdNtLiveStudyDetail = tdNtLiveStudyDao.findTdNtLiveStudyDetail(params);
                    if (tdNtLiveStudyDetail != null) {
                        map.put("is_but", 0);
                    }
                }
                List<TdNtLivePictureList> tdNtLivePictureLists = tdNtLivePictureDao.findTdNtLivePictureByLiveId(tdNtLiveCourseHisDetails.getLive_cou_id());
                map.put("pictures", tdNtLivePictureLists);
                map.put("share",0);
                map.put("share_name",tdNtLiveCourseHisDetails.getLive_cou_his_name());
                map.put("share_url",Constants.share_live_url + tdNtLiveCourseHisDetails.getLive_cou_his_id());
                map.put("share_picutre",tdNtLiveCourseHisDetails.getTea_head_portrait());
                map.put("share_content",tdNtLiveCourseHisDetails.getLive_cou_synopsis());
                String is_collection = "1";
                /**
                 * 查找直播是否有优惠券
                 */
                BigDecimal coupon_amount = new BigDecimal(0);
                if (tdNtMember != null) {
                    Map<String, Object> obj = new HashMap<String, Object>();
                    obj.put("tea_id", tdNtLiveCourseHisDetails.getTea_id());
                    obj.put("memb_id", tdNtMember.getMemb_id());
                    //给查询到的老师教师信息添加收藏字段
                    String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                    if (flag != null) {
                        is_collection = flag;
                    }
                    TdNtCouponDetail detail = new TdNtCouponDetail();
                    detail.setMemb_id(tdNtMember.getMemb_id());
                    detail.setState(1);
                    detail.setType(1);
                    detail.setLimit_id(tdNtLiveCourseHisDetails.getLive_cou_his_id());

                    TdNtCouponDetail couponDetail = tdNtCouponDao.findTdNtCouponDetail(detail);
                    if (couponDetail != null &&
                            (couponDetail.getCoupon_limit_amount().compareTo(new BigDecimal(0)) == 0
                                    || couponDetail.getCoupon_limit_amount().compareTo(tdNtLiveCourseHisDetails.getLive_cou_system_amount()) < 1
                            )
                            ) {
                        coupon_amount = couponDetail.getCoupon_amount();
                    }
                }
                map.put("coupon_amount",coupon_amount);
                map.put("coupon_system_amount",10);
                map.put("is_collection",is_collection);
                /**
                 * 获取礼物和点亮图标
                 */
                List<TsNtLiveGivingList> tsNtLiveGivingLists= (List<TsNtLiveGivingList>)MemcachedUtils.get(MemcachedKey.tsNtLiveGivingLists);
                map.put("tsNtLiveGivingLists", tsNtLiveGivingLists);
                List<TsNtDictIndex> lighticon = (List<TsNtDictIndex>)MemcachedUtils.get(MemcachedKey.lighticon);
                map.put("lighticon", lighticon);
                int a = 0 ;
                Object object = MemcachedUtils.get("live" + live_cou_his_id);
                if (object != null) {
                    a = Integer.valueOf(object.toString());
                }
                map.put("popularity", a);
                map.put("teacher_id", Constants.teacher);
                if (tdNtReserveTimeDao.findTdNtReserveTimeSize(tdNtLiveCourseHisDetails.getTea_id()) == 0){
                    map.put("is_reserve", 1);
                } else {
                    map.put("is_reserve", 0);
                }
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播数据成功");
                retInfo.setObj(map);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("直播不存在");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo findLiveHisByTeaId(Page page, TdNtTeacherResult tdNtTeacherResult) {
        String logInfo = this.getClass().getName() + ":findLiveHisByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("shipin","/resource/waijiaojun/app/live/live.mp4");
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisTeacherListSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveCourseHisTeacherList> tdNtLiveCourseHisTeacherLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisTeacherList(page);
                /**
                 * 在老师端，将直播课的图片跟名称改回老师提供的
                 */
                for (int i = 0; i < tdNtLiveCourseHisTeacherLists.size(); i++) {
                    TdNtLiveCourseHisTeacherList list = tdNtLiveCourseHisTeacherLists.get(i);
                    TdNtLiveCourseDetail tdNtLiveCourseDetail = tdNtLiveCourseDao.findTdNtLiveCourseDetail(list.getLive_cou_id());
                    if(null != tdNtLiveCourseDetail){
                        list.setLive_cou_his_name(tdNtLiveCourseDetail.getLive_cou_name());
                        list.setLive_cou_his_picture(tdNtLiveCourseDetail.getCover());
                    }
                }

                result.put("lives", tdNtLiveCourseHisTeacherLists);
                result.put("totalPage", totalPage);
                /**
                 * 获取礼物和点亮图标
                 */
                List<TsNtLiveGivingList> tsNtLiveGivingLists= (List<TsNtLiveGivingList>)MemcachedUtils.get(MemcachedKey.tsNtLiveGivingLists);
                result.put("tsNtLiveGivingLists", tsNtLiveGivingLists);
                List<TsNtDictIndex> lighticon = (List<TsNtDictIndex>)MemcachedUtils.get(MemcachedKey.lighticon);
                result.put("lighticon", lighticon);
                result.put("share",0);
                result.put("share_name","大开眼界，寓教于乐，体验世界风情和新奇！我正在看外教君直播！");
                result.put("share_url",Constants.share_live_url);
                result.put("share_content",tdNtTeacherResult.getTea_name() + "向你发起了一个英语对话，赶紧点开回复吧！");

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveHisRanking(Page page) {
        String logInfo = this.getClass().getName() + ":findLiveHisRanking:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisRandingSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<Map<String,Object>> list = tdNtLiveCourseHisDao.findTdNtLiveCourseHisRanding(page);
                for (int i=0 ; i < list.size() ; i ++ ) {
                    int memb_id = Integer.valueOf(list.get(i).get("memb_id").toString());
                    TdNtMemberDetail tdNtMemberDetail = tdNtMemberDao.findTdNtMemberDetail(memb_id);
                    list.get(i).put("memb_name", tdNtMemberDetail.getMemb_name());
                    list.get(i).put("memb_head_portrait", tdNtMemberDetail.getMemb_head_portrait());
                }
                result.put("list", list);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取排行榜成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无数据");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveHisUnFinishByMembId(Page page) {
        String logInfo = this.getClass().getName() + ":findLiveHisUnFinishByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByMembIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveCourseHisList> tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByMembIdPage(page);
                for (int i=0 ; i < tdNtLiveCourseHisLists.size() ; i ++ ) {
                    TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtLiveCourseHisLists.get(i).getTea_id());
                    tdNtLiveCourseHisLists.get(i).setTea_head_portrait(tdNtTeacherIndex.getTea_head_portrait());
                    tdNtLiveCourseHisLists.get(i).setTea_name(tdNtTeacherIndex.getTea_name());
                }
                result.put("lives", tdNtLiveCourseHisLists);
                result.put("totalPage", totalPage);
//                /**
//                 * 获取礼物和点亮图标
//                 */
//                List<TsNtLiveGivingList> tsNtLiveGivingLists= (List<TsNtLiveGivingList>)MemcachedUtils.get(MemcachedKey.tsNtLiveGivingLists);
//                result.put("tsNtLiveGivingLists", tsNtLiveGivingLists);
//                List<TsNtDictIndex> lighticon = (List<TsNtDictIndex>)MemcachedUtils.get(MemcachedKey.lighticon);
//                result.put("lighticon", lighticon);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveHisFinishByMembId(Page page) {
        String logInfo = this.getClass().getName() + ":findLiveHisFinishByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByMembIdFinishSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveCourseHisList> tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByMembIdFinishPage(page);
                for (int i=0 ; i < tdNtLiveCourseHisLists.size() ; i ++ ) {
                    TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtLiveCourseHisLists.get(i).getTea_id());
                    tdNtLiveCourseHisLists.get(i).setTea_head_portrait(tdNtTeacherIndex.getTea_head_portrait());
                    tdNtLiveCourseHisLists.get(i).setTea_name(tdNtTeacherIndex.getTea_name());
                }
                result.put("lives", tdNtLiveCourseHisLists);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveHisListByTeaId(Page page,TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findLiveHisListByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord_un = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByTeaIdUnFinishSize(page);
            int totalRecord = totalRecord_un
                    + tdNtLiveCourseHisDao.findTdNtLiveCourseHisByTeaIdFinishSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<TdNtLiveCourseHisMembList> tdNtLiveCourseHisLists;
                if (currentPage * pageSize > totalRecord_un) {
                    int size = currentPage - totalRecord_un/10 -1;
                    if (size == 0) {
                        tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByTeaIdUnFinishPage(page);
                        page.setCurrentPage( size * 10);
                        page.setPageSize(pageSize-totalRecord_un%10);
                        tdNtLiveCourseHisLists.addAll(tdNtLiveCourseHisDao.findTdNtLiveCourseHisByTeaIdFinishPage(page));
                    } else {
                        page.setCurrentPage(size * 10 -totalRecord_un%10);
                        tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByTeaIdFinishPage(page);
                    }

                } else {
                    tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByTeaIdUnFinishPage(page);
                }

                result.put("share_name","大开眼界，寓教于乐，体验世界风情和新奇！我正在看外教君直播！");
                result.put("share_url",Constants.share_live_url);
                result.put("share_content","向你发起了一个英语对话，赶紧点开回复吧！");
                result.put("lives", tdNtLiveCourseHisLists);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveHis(Page page,TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findLiveHis:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();

            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int unfinishedRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListUnSize(page);//直播未完成的条数(包括直播中跟未开始的)

            int totalRecord = unfinishedRecord + tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListSize(page);//总条数

            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }

                page.setCurrentPage((page.getCurrentPage()-1) * pageSize);
                List<TdNtLiveCourseHisNewList> tdNtLiveCourseHisLists;

                if (currentPage * pageSize > unfinishedRecord) {
                    int size = currentPage - (unfinishedRecord / 10 + 1);
                    if (size == 0) {
                        //查询未完成的直播
                        tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListUnList(page);
                        page.setCurrentPage( size * 10);
                        page.setPageSize(pageSize-unfinishedRecord % 10);

                        //查询已完成的直播
                        tdNtLiveCourseHisLists.addAll(tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListList(page));
                    } else {
                        page.setCurrentPage(size * 10 -unfinishedRecord % 10);
                        //查询已完成的直播
                        tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListList(page);

                    }

                } else {
                    //查询未完成的直播
                    tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListUnList(page);
                }

                if (tdNtMember != null && tdNtLiveCourseHisLists != null && tdNtLiveCourseHisLists.size() > 0) {
                    for (int i = 0 ; i < tdNtLiveCourseHisLists.size() ; i ++) {
                        /**
                         * 是否已赞
                         */
                        TdNtLikes likes = new TdNtLikes();
                        likes.setLive_cou_his_id(tdNtLiveCourseHisLists.get(i).getLive_cou_his_id());
                        likes.setMemb_id(tdNtMember.getMemb_id());
                        TdNtLikes tdNtLikes = tdNtLikesDao.findTdNtLikesById(likes);
                        if (tdNtLikes != null && tdNtLikes.getLikes_state() == 0) {
                            tdNtLiveCourseHisLists.get(i).setIs_like(0);
                        }
                        /**
                         * 是否收藏
                         */
                        Map<String, Object> obj = new HashMap<String, Object>();
                        obj.put("tea_id", tdNtLiveCourseHisLists.get(i).getTea_id());
                        obj.put("memb_id", tdNtMember.getMemb_id());
                        //给查询到的老师教师信息添加收藏字段
                        String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                        if (flag != null) {
                            tdNtLiveCourseHisLists.get(i).setCollection_flag(flag);
                        }
                        /**
                         * 查找老师系统标签
                         */
                        Map<String, Object> params = new HashMap<>();
                        params.put("tea_id", tdNtLiveCourseHisLists.get(i).getTea_id());
                        params.put("lab_type", 1);
                        tdNtLiveCourseHisLists.get(i).setLabels(tdNtTeacherLabelDao.findTdNtTeacherListTeaIdAndType(params));
                    }

                }
                result.put("lives", tdNtLiveCourseHisLists);
                result.put("totalPage", totalPage);
                result.put("share_name","大开眼界，寓教于乐，体验世界风情和新奇！我正在看外教君直播！");
                result.put("share_url", Constants.share_live_url);
                result.put("share_content","向你发起了一个英语对话，赶紧点开回复吧！");
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }

            if (page.getObj().equals(Constants.FindLiveType.all)) {
                result.put("live_type", MemcachedUtils.get(MemcachedKey.livetype));
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }



    @Override
    public RetInfo findLiveHisByState(Page page,TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findLiveHis:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            page.setObj(2);
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveCourseHisNewList> tdNtLiveCourseHisTeacherLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisNewListList(page);
                if (tdNtMember != null && tdNtLiveCourseHisTeacherLists != null && tdNtLiveCourseHisTeacherLists.size() > 0) {
                    for (int i = 0 ; i < tdNtLiveCourseHisTeacherLists.size() ; i ++) {
                        TdNtLikes likes = new TdNtLikes();
                        likes.setLive_cou_his_id(tdNtLiveCourseHisTeacherLists.get(i).getLive_cou_his_id());
                        likes.setMemb_id(tdNtMember.getMemb_id());
                        TdNtLikes tdNtLikes = tdNtLikesDao.findTdNtLikesById(likes);
                        if (tdNtLikes != null && tdNtLikes.getLikes_state() == 0) {
                            tdNtLiveCourseHisTeacherLists.get(i).setIs_like(0);
                        }
                        /**
                         * 是否收藏
                         */
                        Map<String, Object> obj = new HashMap<String, Object>();
                        obj.put("tea_id", tdNtLiveCourseHisTeacherLists.get(i).getTea_id());
                        obj.put("memb_id", tdNtMember.getMemb_id());
                        //给查询到的老师教师信息添加收藏字段
                        String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                        if (flag != null) {
                            tdNtLiveCourseHisTeacherLists.get(i).setCollection_flag(flag);
                        }
                    }

                }
                result.put("lives", tdNtLiveCourseHisTeacherLists);
                result.put("totalPage", totalPage);

                result.put("live_type", MemcachedUtils.get(MemcachedKey.livetype));
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveDetailThree(int live_cou_his_id, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findLiveDetailThree:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * 查找直播详情(id,封面,直播名称,时长 ,费用,简介,教师头像,名称,评分,频道)
             */
            TdNtLiveCourseHisDetails tdNtLiveCourseHisDetails = tdNtLiveCourseHisDao.findTdNtLiveCourseHisDetails(live_cou_his_id);
            if (tdNtLiveCourseHisDetails != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("live", tdNtLiveCourseHisDetails);
                /**
                 * 已购买直播人数,直播人头像
                 */
                List<TdNtLiveHisMemberHead> liveHisMemberHeads = tdNtLiveStudyDao.findTdNtLiveHisMemberHeads(live_cou_his_id);
                map.put("memb_head", liveHisMemberHeads);
                /**
                 * 是否购买该直播
                 */
                map.put("is_but", 1);
                if (tdNtMember != null ){
                    Map<String, Integer> params = new HashMap<>();
                    params.put("live_cou_his_id", live_cou_his_id);
                    params.put("memb_id", tdNtMember.getMemb_id());

                    TdNtLiveStudyDetail tdNtLiveStudyDetail = tdNtLiveStudyDao.findTdNtLiveStudyDetail(params);
                    if (tdNtLiveStudyDetail != null) {
                        map.put("is_but", 0);
                    }
                }

                List<TdNtLivePictureList> tdNtLivePictureLists = tdNtLivePictureDao.findTdNtLivePictureByLiveId(tdNtLiveCourseHisDetails.getLive_cou_id());
                map.put("pictures", tdNtLivePictureLists);
                map.put("share",0);
                map.put("share_name",tdNtLiveCourseHisDetails.getLive_cou_his_name());
                map.put("share_url", Constants.share_live_url + tdNtLiveCourseHisDetails.getLive_cou_his_id());
                map.put("share_picutre",tdNtLiveCourseHisDetails.getTea_head_portrait());
                map.put("share_content",tdNtLiveCourseHisDetails.getLive_cou_synopsis());
                map.put("conference_title",tdNtLiveCourseHisDetails.getConference_title());
                map.put("conference_url",tdNtLiveCourseHisDetails.getConference_url());
                map.put("conference_password",tdNtLiveCourseHisDetails.getConference_password());
                String is_collection = "1";
                /**
                 * 查找直播是否有优惠券
                 */
                BigDecimal coupon_amount = new BigDecimal(0);
                if (tdNtMember != null) {
                    Map<String, Object> obj = new HashMap<String, Object>();
                    obj.put("tea_id", tdNtLiveCourseHisDetails.getTea_id());
                    obj.put("memb_id", tdNtMember.getMemb_id());
                    //给查询到的老师教师信息添加收藏字段
                    String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                    if (flag != null) {
                        is_collection = flag;
                    }
                    TdNtCouponDetail detail = new TdNtCouponDetail();
                    detail.setMemb_id(tdNtMember.getMemb_id());
                    detail.setState(1);
                    detail.setType(1);
                    detail.setLimit_id(tdNtLiveCourseHisDetails.getLive_cou_his_id());

                    TdNtCouponDetail couponDetail = tdNtCouponDao.findTdNtCouponDetail(detail);
                    if (couponDetail != null &&
                            (couponDetail.getCoupon_limit_amount().compareTo(new BigDecimal(0)) == 0
                                    || couponDetail.getCoupon_limit_amount().compareTo(tdNtLiveCourseHisDetails.getLive_cou_system_amount()) < 1
                            )
                            ) {
                        coupon_amount = couponDetail.getCoupon_amount();
                    }
                }
                map.put("coupon_amount",coupon_amount);
                map.put("coupon_system_amount",10);
                map.put("is_collection",is_collection);
                /**
                 * 获取礼物和点亮图标
                 */
                List<TsNtLiveGivingList> tsNtLiveGivingLists= (List<TsNtLiveGivingList>)MemcachedUtils.get(MemcachedKey.tsNtLiveGivingLists);
                map.put("tsNtLiveGivingLists", tsNtLiveGivingLists);
                List<TsNtDictIndex> lighticon = (List<TsNtDictIndex>)MemcachedUtils.get(MemcachedKey.lighticon);
                map.put("lighticon", lighticon);
                int a = 0 ;
                Object object = MemcachedUtils.get("live" + live_cou_his_id);
                if (object != null) {
                    a = Integer.valueOf(object.toString());
                }
                map.put("popularity", a);
                map.put("teacher_id", Constants.teacher);
                if (tdNtReserveTimeDao.findTdNtReserveTimeSize(tdNtLiveCourseHisDetails.getTea_id()) == 0){
                    map.put("is_reserve", 1);
                } else {
                    map.put("is_reserve", 0);
                }
                Page page = new Page();
                page.setObj(tdNtLiveCourseHisDetails.getLive_cou_his_id());
                page.setCurrentPage(0);
                List<TdNtLiveCommentList> tdNtLiveCommentLists = tdNtLiveCommentDao.findTdNtLiveCommentByLiveHisIdPage(page);
                for (int i = 0 ; i < tdNtLiveCommentLists.size() ; i++){
                    TdNtLiveCommentList comment = tdNtLiveCommentLists.get(i);
                    String name = CommonUtil.substring(comment.getMemb_name(),0,1) + "****" +
                            CommonUtil.substring(comment.getMemb_name(),comment.getMemb_name().length() - 1,comment.getMemb_name().length());
                    comment.setMemb_name(name);
                }
                map.put("comments", tdNtLiveCommentLists);

                int is_like = 1;//0赞 1取消赞
                if (tdNtMember != null) {
                    TdNtLikes likes = new TdNtLikes();
                    likes.setLive_cou_his_id(tdNtLiveCourseHisDetails.getLive_cou_his_id());
                    likes.setMemb_id(tdNtMember.getMemb_id());
                    TdNtLikes tdNtLikes = tdNtLikesDao.findTdNtLikesById(likes);
                    if (tdNtLikes != null && tdNtLikes.getLikes_state() == 0) {
                        is_like = 0;
                    }
                }
                map.put("is_like", is_like);

                int comment_total_page;
                if (tdNtLiveCourseHisDetails.getComment_size() % page.getPageSize() == 0) {
                    comment_total_page = tdNtLiveCourseHisDetails.getComment_size() / page.getPageSize() ;
                } else {
                    comment_total_page = (tdNtLiveCourseHisDetails.getComment_size() / page.getPageSize() ) + 1;
                }
                map.put("comment_total_page", comment_total_page);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播数据成功");
                retInfo.setObj(map);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("直播不存在");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
